package com.zte.cl.parser.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.log4j.Logger;

import com.zte.cl.utils.StringUtil;

import us.codecraft.webmagic.utils.FilePersistentBase;

public class FileUtil extends FilePersistentBase {

	private final static Logger logger = Logger.getLogger(FileUtil.class);

	public void save(String direcotryPath, String fileName, String content) {
		String filePath = direcotryPath + PATH_SEPERATOR + fileName;
		try {
			PrintWriter printWriter = new PrintWriter(new FileWriter(getFile(filePath)));
			printWriter.println(content);
			printWriter.close();
		} catch (IOException e) {
			logger.warn("write file error", e);
		}
	}
	
	// 获取文件类型
	public static String getFileType(String fileName) {
		int n=fileName.lastIndexOf(".");
		if(n>0){
			 return fileName.substring(n+1,
						fileName.length());
		}
		return "";
	}

	
	
	public static String getImgType(String imgName) {
		int n=imgName.lastIndexOf(".");
		String typeName="";
		if(n>0){
			typeName= imgName.substring(n+1,
					 imgName.length());
		}
		if(StringUtil.isEmpty(typeName)||!"jpg,jpeg,gif,png,bmp,webp".contains(typeName.toLowerCase())){
			typeName="jpg";
		}
		return typeName;
	}
	
	/**
	 * 删除文件
	 * 
	 * @param file
	 *            文件全路径
	 */
	public static void deleteFile(File file) {
		if (file.exists()) {
			file.delete();
		}
	}
}
